package com.ler.yrmall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ler.yrmall.product.entity.SpuImagesEntity;
import com.ler.yrmall.product.mapper.SpuImagesMapper;
import com.ler.yrmall.product.service.SpuImagesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author leron
 */
@Service
public class SpuImagesServiceImpl extends ServiceImpl<SpuImagesMapper, SpuImagesEntity> implements SpuImagesService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveImages(Long id, List<String> images) {
        this.remove(Wrappers.<SpuImagesEntity>lambdaQuery().eq(SpuImagesEntity::getSpuId,id));
        if (images.size() != 0){
            List<SpuImagesEntity> collect = images.stream().map(item -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(id);
                spuImagesEntity.setImgUrl(item);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }

    @Override
    public List<SpuImagesEntity> listBySpuId(Long spuId) {
        return this.list(Wrappers.<SpuImagesEntity>lambdaQuery().eq(SpuImagesEntity::getSpuId, spuId));
    }

    @Override
    public void deleteBySpuId(long spuId) {
        this.remove(Wrappers.<SpuImagesEntity>lambdaQuery().eq(SpuImagesEntity::getSpuId,spuId));
    }

}